﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Project.Data;
using Project.Data.Repository;
using Project.Model.EF6.Entity;
using Project.Services.Tools;

namespace Project.Services
{
    public static  class ClearData
    {
        public static bool Delete(DateTime date)
        {
            try
            {
                var flag = true;
                var rep = new DbContextRepository<EfContext>();
                var redDay = rep.FindAll<RedDay>(m => m.ROW_UPDATE_DATE == date);
                var amountdDay = rep.FindAll<AmountDay>(m => m.ROW_UPDATE_DATE == date);
                var peopleDay = rep.FindAll<PeopleDay>(m => m.ROW_UPDATE_DATE == date);
                var pointDay = rep.FindAll<IntegralDay>(m => m.ROW_UPDATE_DATE == date);
                var distributorDay = rep.FindAll<DistributorDay>(m => m.ROW_UPDATE_DATE == date);

                if (redDay.Count > 0)
                {
                    //删除和更新红包次数表
                    var redWeek = rep.FindAll<RedWeek>(m => date >= m.WEEK_BEGIN && date <= m.WEEK_END);
                    var redMonth = rep.FindAll<RedMonth>(m => date >= m.MONTH_BEGIN && date <= m.MONTH_END);
                    foreach (var n in redDay)
                    {
                        var temp = redWeek.Where(m => m.PROVINCE == n.PROVINCE).ToList().FirstOrDefault();
                        if (temp != null)
                        {
                            temp.COUNT -= n.COUNT;
                            rep.Update<RedWeek>(temp, false);
                        }
                        var monthTemp = redMonth.Where(m => m.PROVINCE == n.PROVINCE).ToList().FirstOrDefault();
                        if (monthTemp != null)
                        {
                            monthTemp.COUNT -= n.COUNT;
                            rep.Update<RedMonth>(monthTemp, false);
                        }
                    }
                    rep.Delete<RedSemih>(m => m.ROW_UPDATE_DATE == date, false);
                    flag=rep.Delete<RedDay>(m => m.ROW_UPDATE_DATE == date);
                }

                if (amountdDay.Count > 0)
                {
                    //删除和更新红包金额表
                    var amountWeek = rep.FindAll<AmountWeek>(m => date >= m.WEEK_BEGIN && date <= m.WEEK_END);
                    var amountMonth = rep.FindAll<AmountMonth>(m => date >= m.MONTH_BEGIN && date <= m.MONTH_END);
                    foreach (var n in amountdDay)
                    {
                        var temp = amountWeek.Where(m => m.PROVINCE == n.PROVINCE).ToList().FirstOrDefault();
                        if (temp != null)
                        {
                            temp.AMOUNT -= n.AMOUNT;
                            rep.Update<AmountWeek>(temp, false);
                        }
                        var monthTemp = amountMonth.Where(m => m.PROVINCE == n.PROVINCE).ToList().FirstOrDefault();
                        if (monthTemp != null)
                        {
                            monthTemp.AMOUNT -= n.AMOUNT;
                            rep.Update<AmountMonth>(monthTemp, false);
                        }
                    }
                    rep.Delete<AmountSemih>(m => m.ROW_UPDATE_DATE == date, false);
                    flag=rep.Delete<AmountDay>(m => m.ROW_UPDATE_DATE == date);
                }

                if (peopleDay.Count > 0)
                {
                    //删除和更新参与人数表
                    var peopleWeek = rep.FindAll<PeopleWeek>(m => date >= m.WEEK_BEGIN && date <= m.WEEK_END);
                    var peopleMonth = rep.FindAll<PeopleMonth>(m => date >= m.MONTH_BEGIN && date <= m.MONTH_END);
                    foreach (var n in peopleDay)
                    {
                        var temp = peopleWeek.Where(m => m.PROVINCE == n.PROVINCE).ToList().FirstOrDefault();
                        if (temp != null)
                        {
                            temp.COUNT -= n.COUNT;
                            rep.Update<PeopleWeek>(temp, false);
                        }
                        var monthTemp = peopleMonth.Where(m => m.PROVINCE == n.PROVINCE).ToList().FirstOrDefault();
                        if (monthTemp != null)
                        {
                            monthTemp.COUNT -= n.COUNT;
                            rep.Update<PeopleMonth>(monthTemp, false);
                        }
                    }
                    rep.Delete<PeopleSemih>(m => m.ROW_UPDATE_DATE == date, false);
                    flag = rep.Delete<PeopleDay>(m => m.ROW_UPDATE_DATE == date);
                }

                if (pointDay.Count > 0)
                {
                    //删除和更新积分发放表
                    var pointWeek = rep.FindAll<IntegralWeek>(m => date >= m.WEEK_BEGIN && date <= m.WEEK_END);
                    var pointMonth = rep.FindAll<IntegralMonth>(m => date >= m.MONTH_BEGIN && date <= m.MONTH_END);
                    foreach (var n in pointDay)
                    {
                        var temp = pointWeek.Where(m => m.PROVINCE == n.PROVINCE).ToList().FirstOrDefault();
                        if (temp != null)
                        {
                            temp.COUNT -= n.COUNT;
                            rep.Update<IntegralWeek>(temp, false);
                        }
                        var monthTemp = pointMonth.Where(m => m.PROVINCE == n.PROVINCE).ToList().FirstOrDefault();
                        if (monthTemp != null)
                        {
                            monthTemp.COUNT -= n.COUNT;
                            rep.Update<IntegralMonth>(monthTemp, false);
                        }
                    }
                    rep.Delete<IntegralSemih>(m => m.ROW_UPDATE_DATE == date, false);
                    flag = rep.Delete<IntegralDay>(m => m.ROW_UPDATE_DATE == date);
                }

                if (distributorDay.Count > 0)
                {
                    //删除和更新经销商表
                    var distributorWeek = rep.FindAll<DistributorWeek>(m => date >= m.WEEK_BEGIN && date <= m.WEEK_END);
                    var distributorMonth = rep.FindAll<DistributorMonth>(m => date >= m.MONTH_BEGIN && date <= m.MONTH_END);
                    foreach (var n in distributorDay)
                    {
                        var temp = distributorWeek.Where(m => m.PROVINCE == n.PROVINCE).ToList().FirstOrDefault();
                        if (temp != null)
                        {
                            temp.COUNT -= n.COUNT;
                            rep.Update<DistributorWeek>(temp, false);
                        }
                        var monthTemp = distributorMonth.Where(m => m.PROVINCE == n.PROVINCE).ToList().FirstOrDefault();
                        if (monthTemp != null)
                        {
                            monthTemp.COUNT -= n.COUNT;
                            rep.Update<DistributorMonth>(monthTemp, false);
                        }
                    }
                    rep.Delete<DistributorSemih>(m => m.ROW_UPDATE_DATE == date, false);
                    return  rep.Delete<DistributorDay>(m => m.ROW_UPDATE_DATE == date);
                }
                return flag;
            }
            catch (Exception ex) 
            {
                ex.ErrorHandle();
                return false;
            }
        }
    }
}
